"""
学生视图层
"""

from lib import common
from interface import student_interface, common_interface

user_dict = {
    'user': None,
}


# 学生注册函数
def register():
    while True:
        # 1. 接受用户输入
        username = input("请输入用户名: ").strip()
        password = input("请输入密码: ").strip()
        re_password = input("请确认密码: ").strip()

        # 2. 校验用户输入
        if password != re_password:
            print("两次密码不一致！")
            continue

        # 3. 调用接口函数
        flag, msg = student_interface.register_interface(username, password)

        # 4. 显示返回结果
        if flag:
            print(msg)
            break
        else:
            print(msg)


# 学生登录函数
def login():
    while True:
        # 1. 接受用户输入
        username = input("请输入用户名: ").strip()
        password = input("请输入密码: ").strip()

        # 2. 校验用户输入
        if username == "" or password == "":
            print("用户名或密码不能为空！")
            continue

        # 3. 调用接口函数
        flag, msg = common_interface.login_common_interface(username, password, user_type='student')

        # 4. 显示返回结果
        if flag:
            print(msg)
            user_dict["user"] = username
            break
        else:
            print(msg)


# 学生选择学校函数
@common.auth('student')
def select_school():
    while True:
        # 1. 打印出所有的学校
        flag, msg = common_interface.get_all_school_interface()

        # 2. 如果没有学校则退出
        if not flag:
            print(msg)
            break

        # 3. 如果有学校则打印并让用户选择
        print("当前系统中的学校有: ")
        for key, value in enumerate(msg):
            print(f"\t{key}.{value}")
        choice = input("请输入编号选择要添加的学校: ").strip()

        if not choice.isdigit():
            print("必须输入数字")
            continue

        choice = int(choice)
        select_school = msg[choice]

        # 4. 调用接口函数
        flag, msg = student_interface.select_school_interface(
            select_school, user_dict["user"]
        )

        # 5. 显示返回结果
        print(msg)
        break


# 学生选择课程函数
@common.auth('student')
def select_course():
    while True:
        # 1. 打印出学生所在学校和所在学校开设的课程
        # 2. 如果没有学校或所选学校没有开设课程
        current_school = student_interface.get_student_school_interface(user_dict.get("user"))
        if current_school == None:
            print("您当前没有选择学校，请先选择学校！")
            break
        else:
            print(f"您当前校区为 [{current_school}]，可选的课程有: ")
            flag, msg = common_interface.get_course_in_school_interface(current_school)
            if not flag:
                print(msg)
                break
            for key,value in enumerate(msg):
                print(f"\t{key}.{value}")

        # 3. 接受用户选择
        choice = input("请输入编号选择课程: ").strip()

        if not choice.isdigit():
            print("必须输入数字")
            continue

        choice = int(choice)
        select_course = msg[choice]

        # 4. 调用接口函数
        msg = student_interface.select_course_interface(
            select_course, user_dict["user"]
        )

        # 5. 显示返回结果
        print(msg)
        break


# 学生查看分数函数
@common.auth('student')
def check_score():
    msg = student_interface.check_score_interface(user_dict["user"])
    if msg:
        for key,value in msg.items():
            print(f"{key}: {value}")
    else:
        print("当前还没有课程！")


# 功能字典: 菜单，函数
func_dict = {
    "1": ("注册", register),
    "2": ("登录", login),
    "3": ("选择校区", select_school),
    "4": ("选择课程", select_course),
    "5": ("查看分数", check_score)
}


def student_view():
    while True:
        print("")
        print(" 欢迎来到学生界面 ".center(30, '='))
        for key,value in func_dict.items():
            print(f"\t    {key}.{value[0]}")
        print(" end ".center(38, '='))
        print("")

        choice = input("请输入功能编码(q退出): ").strip()

        if choice == 'q':
            break

        if choice not in func_dict:
            print("输入的值超出范围！")
            continue

        func_dict[choice][1]()